﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using MyWindowsMediaPlayer.Libs.Mediator;
using MyWindowsMediaPlayer.Libs.ViewModel;
using MyWindowsMediaPlayer.Models;
using MyWindowsMediaPlayer.Models.Library;

namespace MyWindowsMediaPlayer.ViewModels.Center
{
	public class CenterViewModel : ViewModelBase
	{
		#region Members

		ObservableCollection<MediaViewModel>[] _items;
		ObservableCollection<MediaViewModel> _currentItem;
		ICollectionView _collectionView;
		Mediator _mediator;
		Visibility _mediaElementVisibility;
		Visibility _listViewVisibility;
		Uri _mediaUri;
		MediaElement _mElement;
		int _index;
		int _indexTmp;
		#endregion

		#region Constructors
		
		public CenterViewModel ( Mediator mediator, MediaElement mElement )
		{
			this._items = new ObservableCollection<MediaViewModel>[]{
				new ObservableCollection<MediaViewModel>(),
 				new ObservableCollection<MediaViewModel>(),
				new ObservableCollection<MediaViewModel>()
			};

			this._collectionView = CollectionViewSource.GetDefaultView(this._items);
			this._mediator = mediator;
			this._mediaElementVisibility = Visibility.Hidden;
			this._listViewVisibility = Visibility.Visible;
			this._mElement = mElement;

			this.subscribeMediator();

			foreach (MediaModel media in DatabaseMedia.parseXMLFile("Music.xml"))
				_items[ 0 ].Add(new MediaViewModel(media, this._mediator));
			foreach (MediaModel media in DatabaseMedia.parseXMLFile("Movie.xml"))
				_items[ 1 ].Add(new MediaViewModel(media, this._mediator));
			foreach (MediaModel media in DatabaseMedia.parseXMLFile("Picture.xml"))
				_items[ 2 ].Add(new MediaViewModel(media, this._mediator));

			this.Items = this._items[ 0 ];
		}

		private void subscribeMediator ( )
		{
			this._mediator.Register(MediatorMessage.PlayItem, param => { this.PlayItem((Uri)param); });
			this._mediator.Register(MediatorMessage.StopItem, param => { this.StopItem(param); });
			this._mediator.Register(MediatorMessage.LibraryItemSelected,
				param =>
				{
					this.MediaElementVisibility = Visibility.Hidden;
					this.ListViewVisibility = Visibility.Visible;
					if ((MediaChoice)param == MediaChoice.Music)
						this.Items = this._items[ 0 ];
					else if ((MediaChoice)param == MediaChoice.Movie)
						this.Items = this._items[ 1 ];
					else if ((MediaChoice)param == MediaChoice.Picture)
						this.Items = this._items[ 2 ];
				});
			this._mediator.Register(MediatorMessage.AddMusic,
				param =>
				{
					this.AddMusic((string)param);
				});
			this._mediator.Register(MediatorMessage.AddMovie,
			param =>
			{
				this.AddMovie((string)param);
				
			});
			this._mediator.Register(MediatorMessage.AddPicture,
			param =>
			{
				this.AddPicture((string)param);
			});

		}

		private void AddMovie ( string path )
		{
			List<MediaModel> tmp = DatabaseMedia.FillXML(path, "Movie.xml", 1);
			foreach (MediaModel media in tmp)
				this._items[ 1 ].Add(new MediaViewModel(media, this._mediator));
		}

		private void AddMusic ( string path )
		{
			List<MediaModel> tmp = DatabaseMedia.FillXML(path, "Music.xml", 0);
			foreach (MediaModel media in tmp)
				this._items[ 0 ].Add(new MediaViewModel(media, this._mediator));
		}

		private void AddPicture ( string path )
		{
			List<MediaModel> tmp = DatabaseMedia.FillXML(path, "Picture.xml", 2);
			foreach (MediaModel media in tmp)
				this._items[ 2 ].Add(new MediaViewModel(media, this._mediator));
		}

		#endregion

		#region Accessors

		public ObservableCollection<MediaViewModel> Items
		{
			set
			{
				this._currentItem = value;
				OnPropertyChanged("Items");
			}
			get { return this._currentItem; }
		}

		public MediaViewModel SelectedMedia
		{
			get { return this._collectionView.CurrentItem as MediaViewModel; }
		}

		public Visibility MediaElementVisibility
		{
			get { return this._mediaElementVisibility;  }
			set
			{
				this._mediaElementVisibility = value;
				OnPropertyChanged("MediaElementVisibility");
			}
		}

		public Visibility ListViewVisibility
		{
			get { return this._listViewVisibility; }
			set
			{
				this._listViewVisibility = value;
				OnPropertyChanged("ListViewVisibility");
			}
		}

		public Uri MediaUri
		{
			get { return this._mediaUri; }
			set
			{
				this._mediaUri = value;
				OnPropertyChanged("MediaUri");
			}
		}

		#endregion

		#region MediaAction

		private void PlayItem ( Uri uri )
		{
			this.MediaElementVisibility = Visibility.Visible;
			this.ListViewVisibility = Visibility.Hidden;

			 this.MediaUri = uri;
			this._mElement.Play();
		}

		private void StopItem ( object obj )
		{
			this._mElement.Stop();
		}

		#endregion

	}
}
